/*
 * Copyright 2019 Zhenjie Yan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.jingli;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.mingmen.canting.R;
import com.mingmen.canting.definerecycleview.ExpandableAdapter;
import com.mingmen.canting.model.Group;
import com.mingmen.canting.model.GroupMember;
import com.mingmen.canting.model.JingliCaiGouDanModel;

import java.util.List;

/**
 * Created by Zhenjie Yan on 1/30/19.
 */
public class MyExpandedAdapter extends ExpandableAdapter<ExpandableAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private List<JingliCaiGouDanModel.DataBean> mGroupList;
    private int checkedPosition = 0;
    Context context;
    private int checkedPosition_parentposition;

    public MyExpandedAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setGroupList(List<JingliCaiGouDanModel.DataBean> groupList) {
        this.mGroupList = groupList;
    }

    @Override
    public int parentItemCount() {
        return mGroupList == null ? 0 : mGroupList.size();
    }

    public void setCheckedPosition(int checkedPosition, int checkedPosition_parentposition) {
        this.checkedPosition = checkedPosition;
        this.checkedPosition_parentposition = checkedPosition_parentposition;
        notifyDataSetChanged();
    }

    @Override
    public int childItemCount(int parentPosition) {
//        List<GroupMember> memberList = mGroupList.get(parentPosition).getMemberList();
        List<JingliCaiGouDanModel.DataBean.ApplyListBean> memberList = mGroupList.get(parentPosition).getApplyList();
        return memberList == null ? 0 : memberList.size();
    }

    @Override
    public ViewHolder createParentHolder(@NonNull ViewGroup root, int viewType) {
        View view = mInflater.inflate(R.layout.item_jingli_expand_parent, root, false);
        return new ParentHolder(view, this);
    }

    @Override
    public ViewHolder createChildHolder(@NonNull ViewGroup root, int viewType) {
        View view = mInflater.inflate(R.layout.item_jingli_expand_child, root, false);
        return new ChildHolder(view, this);
    }

    @Override
    public void bindParentHolder(@NonNull ViewHolder holder, int position) {
//        ((ParentHolder) holder).setData(mGroupList.get(position));
//        ((ParentHolder) holder).setData(mGroupList.get(position));
        ParentHolder myholder = (ParentHolder) holder;
        myholder.mTvTitle.setText(mGroupList.get(position).getLimitData());
//        myholder.setSelected(data.isExpanded());
        myholder.mIvStatus.setSelected(mGroupList.get(position).isExpanded());
        String status = mGroupList.get(position).getApplyList().get(0).getStatus();

        JingliCaiGouDanModel.DataBean data = mGroupList.get(position);
        if (status.equals("0")) {
            myholder.text_status.setText("采购中");
        } else {
            myholder.text_status.setText("已收货");
        }
        if (data.isExpanded()) {
            myholder.layout_parent.setBackground(context.getResources().getDrawable(R.drawable.shape_jingli_dingdan_item2));
        } else {
            myholder.layout_parent.setBackground(context.getResources().getDrawable(R.drawable.shape_jinglii_dingdan_item));
        }
        Log.d("kdkdkdkdk", data.isExpanded() + "");


    }

    @Override
    public void bindChildHolder(@NonNull ViewHolder holder, int parentPosition, int position) {
//        ((ChildHolder) holder).setData(mGroupList.get(parentPosition).getMemberList().get(position));
        JingliCaiGouDanModel.DataBean.ApplyListBean.DetailsBean applyListBean = mGroupList.get(parentPosition).getApplyList().get(position).getDetails().get(position);
        ChildHolder childHolder = (ChildHolder) holder;

        Log.d("checkedPositioncheckedPosition", checkedPosition + "");
        if (applyListBean.getStatus().equals("0")) {
            childHolder.layout_item.setBackgroundColor(Color.parseColor("#FFF3F3"));

//            childHolder.view_select.setVisibility(View.GONE);
            childHolder.view_select.setVisibility(View.GONE);
        } else {
            childHolder.layout_item.setBackgroundColor(Color.parseColor("#FFFFFF"));
            childHolder.view_select.setVisibility(View.GONE);
        }
        if (position == checkedPosition && checkedPosition_parentposition == parentPosition) {
            childHolder.view_select.setVisibility(View.VISIBLE);

        } else {
            childHolder.view_select.setVisibility(View.GONE);
        }


        /*if (position == checkedPosition) {
            childHolder.layout_item.setBackgroundColor(Color.parseColor("#FFF3F3"));
            childHolder.view_select.setVisibility(View.GONE);

        } else {
            childHolder.layout_item.setBackgroundColor(Color.parseColor("#FFFFFF"));
//            childHolder.view_select.setVisibility(View.GONE);
            childHolder.view_select.setVisibility(View.GONE);
        }*/
//        childHolder.mTvTitle.setText(mGroupList.get(parentPosition).getMemberList().get(position).getName());
//        mTvTitle.setText(data.getName());
        childHolder.mTvTitle.setText(applyListBean.getCommodityName());
        childHolder.applyAmount.setText("数量:" + applyListBean.getApplyAmount() + "");
        childHolder.text_mark.setText(applyListBean.getMark());
        int childsize = mGroupList.get(parentPosition).getApplyList().size();
        if (position == childsize - 1) {
            childHolder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem2));
        } else {
            childHolder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_jingli_dingdan_ziitem3));
        }

    }

    static class ParentHolder extends ViewHolder {

        TextView mTvTitle;
        ImageView mIvStatus;
        TextView text_status;
        RelativeLayout layout_parent;

        public ParentHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            mIvStatus = itemView.findViewById(R.id.iv_status);
            text_status = itemView.findViewById(R.id.text_status);
            layout_parent = itemView.findViewById(R.id.layout_parent);
        }

     /*   public void setData(Group data) {
            mTvTitle.setText(data.getName());
            mIvStatus.setSelected(data.isExpanded());
        }*/
    }

    static class ChildHolder extends ViewHolder {

        TextView mTvTitle;
        LinearLayout layout_item;
        View view_select;
        TextView applyAmount;
        TextView text_mark;

        public ChildHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            layout_item = itemView.findViewById(R.id.layout_item);
            view_select = itemView.findViewById(R.id.view_select);
            applyAmount = itemView.findViewById(R.id.applyAmount);
            text_mark = itemView.findViewById(R.id.text_mark);
        }

        public void setData(GroupMember data) {
            mTvTitle.setText(data.getName());
        }
    }
}